package service;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.*;
import domain.Track;

public class SimpleTrackManager implements TrackManager {
  private List<Track> elements;
  private List<Track> tracksAlbum;

  public List<Track> getAll() {
    return elements;
  }

  public void setTracksAlbum(List<Track> tracksAlbum){this.tracksAlbum=tracksAlbum;}
  public List<Track> getTracksAlbum(){return this.tracksAlbum;}

  public void setTracks(List<Track> elements) {
    this.elements = elements;
  }
  public List<Track> getTracks(){return this.elements;}
  public Track getById(String id) {
    Iterator itr = elements.iterator();
    Track element;
    while (itr.hasNext()) {
      element = (Track)itr.next();
      if (element.getPk_track().equals(id)) return element;
    }
    return null;
  }
  
  public void delete(String id){

    Iterator itr = elements.iterator();

    while (itr.hasNext()) {
      Track element;
      element = (Track)itr.next();
      if (element.getPk_track().equals(id)){
        itr.remove();
  	  }
    }
  }
  
  public void insert(Track myObj){

    if (myObj.getNombre()!=null){
      Random generator = new Random();
      int id = generator.nextInt();
	    id = Math.abs(id);
		  myObj.setPk_track(String.valueOf(id));
		  elements.add(myObj);
    }
  }

  public List<Track> getAllByCDID(String id){
    System.out.println("ID getByCDID: "+id);
    tracksAlbum.clear();
    Iterator itr = elements.iterator();
    Track element;
    while (itr.hasNext()) {
      element = (Track)itr.next();
      if (element.getFk_CD().equals(id))
        tracksAlbum.add(element);      
    }
    return tracksAlbum;
  }
}